<template>
  <div class="app-container">
    <!--    <el-form :inline="true" :model="searchFrom" class="demo-form-inline" size="small">-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.number" placeholder="用户编号" />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.name" placeholder="用户名称" />-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" @click="onSearch">查询</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          {{ scope.row.demandId }}
        </template>
      </el-table-column>
      <el-table-column label="举报人头像" width="110" align="center">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="40" fit="fit" :src="scope.row.avatar"/>
        </template>
      </el-table-column>
      <el-table-column label="举报人信息" align="center">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center">
        <template slot-scope="scope">
          {{ scope.row.demandName }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center">
        <template slot-scope="scope">
          {{ scope.row.demandTitle }}
        </template>
      </el-table-column>
      <el-table-column label="举报原因" align="center">
        <template slot-scope="scope">
          {{ scope.row.reportReply }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="scope">
          {{ scope.row.reportDesc }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="进度" width="110" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.reportProgress | demandFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.reportDate }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="基本操作" width="200">
        <template slot-scope="scope">
          <el-button v-if="scope.row.reportProgress === 0" type="primary" size="mini" @click="blocking(scope.row,1)">
            举报成功
          </el-button>
          <el-button v-if="scope.row.reportProgress === 0" type="primary" size="mini" @click="blocking(scope.row,2)">
            举报失败
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevChange"
        @next-click="handleNextChange"
      />
    </div>
  </div>
</template>

<script>

import { getDemandReportPage, updateDemandReport } from '@/api/Report'
import { updateDemand } from '@/api/Demand'

export default {
  filters: {
    demandFilter(status) {
      const statusMap = {
        0: '未处理',
        10: '已处理'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      searchFrom: {
        name: '',
        number: '',
        email: '',
        type: 0
      },
      list: null,
      listLoading: true,
      total: 0,
      limit: 10,
      page: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const searchParams = this.searchFrom
      searchParams['page'] = this.page
      searchParams['limit'] = this.limit
      getDemandReportPage(searchParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    onSearch() {
      this.fetchData()
    },
    /* 上一页*/
    handlePrevChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 下一页*/
    handleNextChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 数字点击*/
    handleCurrentChange(event) {
      this.page = event
      this.fetchData()
    },
    handleSizeChange(event) {
      this.limit = event
      this.fetchData()
    },
    edit(event) {
      this.$router.push({
        path: '/user/create',
        query: {
          userId: event.userId
        }
      })
    },
    blocking(event, type) {
      const reportFrom = {
        reportId: event.reportId,
        reportProgress: 10
      }
      if (type === 1) {
        updateDemandReport(reportFrom).then(res => {
          if (res.code === 200) {
            const demandFrom = {
              demandId: event.demandId,
              demandReply: null,
              demandAudit: null
            }
            demandFrom.jobReply = event.reportReply
            demandFrom.demandAudit = 40
            updateDemand(demandFrom).then(res => {
              if (res.code === 200) {
                this.$message.success('已冻结')
                this.fetchData()
              } else {
                this.$message.error('网络错误')
              }
            })
          } else {
            this.$message.error('网络错误')
          }
        })
      } else {
        updateDemandReport(reportFrom).then(res => {
          if (res.code === 200) {
            this.$message.success('举报失败')
            this.fetchData()
          } else {
            this.$message.error('网络错误')
          }
        })
      }
      // const demandFrom = {
      //   demandId: event.demandId,
      //   demandReply: null,
      //   demandAudit: null
      // }
      // if (type === 1) {
      //   demandFrom.demandAudit = 40
      //   this.$prompt('请输入冻结理由', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消'
      //   }).then(({ value }) => {
      //     demandFrom.jobReply = value
      //     updateDemandReport(demandFrom).then(res => {
      //       if (res.code === 200) {
      //         this.$message.success('已冻结')
      //         this.fetchData()
      //       } else {
      //         this.$message.error('网络错误')
      //       }
      //     })
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '取消输入'
      //     })
      //   })
      // } else {
      //   demandFrom.demandAudit = 30
      //   updateDemand(demandFrom).then(res => {
      //     if (res.code === 200) {
      //       this.$message.success('取消成功')
      //       this.fetchData()
      //     } else {
      //       this.$message.error('网络错误')
      //     }
      //   })
      // }
    }
  }
}
</script>
